<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <title>DMDirc :: Admin</title>
  <link rel="stylesheet" type="text/css" href="/res/style.css">
 </head>
 <body>
  <div id="container">
   <img id="logo" src="/res/logo.png" alt="DMDirc logo">
   <h1>DMDirc</h1>
<?PHP require_once('admin-menu.php'); ?>
   <div id="content">
    <h2>Build Identity Packs</h2>
<?PHP
 if (isset($_POST['push'])) {
  define('WHAT', $_POST['push']);

  echo '<pre>';

  chdir('../updates/');

  echo "Performing an SVN update...\n\n";

  echo `svn update` . "\n";

  if (WHAT == 'modealiases') {
   $file = 'modealiases/generic';
  } else if (WHAT == 'defaults') {
   $file = 'default/defaults';
  }

  $data = file_get_contents($file);

  if (preg_match('#identity.(.*?)version = ([0-9]+)#i', $data, $m)) {
   $version = ((int) $m[2]) + 1;

   echo "Creating version $version...\n";

   $data = str_replace($m[0], 'identity.' . $m[1] . 'version = ' . $version, $data);

   file_put_contents($file, $data);

   echo "Done editing version number, zipping...\n\n";

   if (WHAT == 'modealiases') {
    echo `zip modealiases.zip modealiases/*`;
    $target = 'modealiases';
   } else if (WHAT == 'defaults') {
    echo `zip defaultsettings.zip default/*`;
    $target = 'defaultsettings';
   }

   $targets = array();
   if (isset($_POST['stable'])) { $targets[] = 'STABLE'; }
   if (isset($_POST['unstable'])) { $targets[] = 'UNSTABLE'; }
   if (isset($_POST['nightly'])) { $targets[] = 'NIGHTLY'; }
   if (isset($_POST['none'])) { $targets[] = 'NONE'; }

   foreach ($targets as $channel) {
    echo "Copying file to settings/$target-$channel-$version.zip\n";
    copy($target . '.zip', "settings/$target-$channel-$version.zip");
   }

   echo "Done!\n";
  } else {
   echo "Unable to read version number\n";
  }

  echo '</pre>';

 } else { ?>
    <form action="buildmodealiases" method="post">
    <h3>Build identity packs</h3>
    <p>
     This will build a release of an identity pack. Avoid doing this too often 
     or if the contents haven't changed, as clients will prompt users to update. 
    </p>
    <table>
     <tr><td>Identity pack</td><td><select name="push">
      <option value="modealiases">Mode Aliases</option>
      <option value="defaults">Default Settings</option>
     </select></td></tr>
     <tr><td>Client versions</td><td>
      <label><input type="checkbox" name="stable"> Stable</label>
      <label><input type="checkbox" name="unstable"> Unstable</label>
      <label><input type="checkbox" name="nightly" checked="checked"> Nightly</label>
      <label><input type="checkbox" name="none" checked="checked"> None (SVN)</label>
     </td></tr>
     <tr><td></td><td><input type="submit" name="submit" value="Push"></td></tr>
    </table>
<?PHP } ?>
   </div>
   <div id="footer">
   </div>
  </div>
 </body>
</html>

